package com.qf.taobei.service.impl;

import com.qf.taobei.entity.Address;
import com.qf.taobei.mapper.AddressMapper;
import com.qf.taobei.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public Integer addAddress(Address address) {
        return addressMapper.addAddress(address);
    }

    @Override
    public Integer updateAddress(Address address) {
        return addressMapper.updateAddress(address);
    }

    @Override
    public List<Address> queryAllAddress(Integer id) {
        return addressMapper.queryAllAddress(id);
    }


    @Override
    public Integer updateAddrDef(Integer uid, Integer id) {
        //先根据用户id查询用户所有的收货地址
        List<Address> addressList = queryAllAddress(uid);
        //将原来的默认收货地址改成非默认
        for (Address addr : addressList) {
            if (addr.getFlag() == 0){
                addressMapper.updateAddrDef(addr.getId(),1);
            }
        }
        //设置指定的地址为默认收货地址
        return addressMapper.updateAddrDef(id,0);
    }

    @Override
    public Integer delAddr(Integer id) {
        return addressMapper.delAddressById(id);
    }
}
